package org.bolmart.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.flex.remoting.RemotingInclude;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;

@Service
@RemotingDestination
public class UsersDao {

    private static HibernateTemplate hibernateTemplate;

    @Autowired
    public void setHibernateTemplate(HibernateTemplate hibernateTemplate){
        this.hibernateTemplate = hibernateTemplate;
    }
    @Autowired
    public HibernateTemplate getHibernateTemplate(){
        return hibernateTemplate;
    }
    @RemotingInclude
    public static Customer getCustomer(final int idusers){        
        HibernateCallback callback = new HibernateCallback() {
            public Object doInHibernate(Session session) 
                throws HibernateException,SQLException {
                //return session.load(Users.class, idusers);
                return session.get(Customer.class, idusers);
            }
        };        
        return (Customer)hibernateTemplate.execute(callback);
    }
//    @RemotingInclude
//    public static List<Customer> getCustomers(){        
//        HibernateCallback callback = new HibernateCallback() {
//            public Object doInHibernate(Session session) 
//                throws HibernateException,SQLException {
//                //return session.load(Users.class, idusers);
//                return session..get(Customer.class, idusers);
//            }
//        };        
//        return (Customer)hibernateTemplate.execute(callback);
//    }
    @RemotingInclude
    public static void saveOrUpdate(final Customer customer){
        HibernateCallback callback = new HibernateCallback() {
            public Object doInHibernate(Session session) 
                throws HibernateException,SQLException {
                session.saveOrUpdate(customer);
                return null;
            }
        };          
        hibernateTemplate.execute(callback);
    }

}
